package com.mapper;

import com.domain.BlogArticle;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface BlogArticleMapper {
    @Select("SELECT * FROM t_article WHERE id = #{id}")
    public BlogArticle selectArticleWithId(Integer id);

    @Insert("INSERT INTO t_article (title, created, modified, tags, categories,"+
            " allow_comment, thumbnail, content)"+
            " VALUES (#{title},#{created}, #{modified}, #{tags},#{categories},"+
            " #{allowComment}, #{thumbnail}, #{content})")
    @Options(useGeneratedKeys = true, keyProperty="id", keyColumn="id")
    public int publishArticle(BlogArticle article);

    @Select("SELECT * FROM t_article ORDER BY id DESC")
    public List<BlogArticle> selectArticleWithPage();

    @Delete("DELETE FROM t_article WHERE id=#{id}")
    public void deleteArticleWithId(int id);

    @Select("SELECT COUNT(1) FROM t_article")
    public int countArticle();

//    @Update("UPDATE t_article SET title={title},created=#{created},tags = #{tags}, thumbanil = #{thumbnail}, content =#{content},"+
//            " categories = #{categories}, hits = #{hits},comments_num = #{commentsNum}, allow_comment = #{allowComment} WHERE id=#{id}")
//    public Integer updateArticleWithId(BlogArticle article);
    @Update({
            "<script>",
            "UPDATE t_article",
            "<set>",
            "<if test='title !=  null'> title=#{title},</if>",
            "<if test='content !=  null'> content=#{content},</if>",
            "<if test='created !=  null'> created=#{created},</if>",
            "<if test='categories !=  null'> categories=#{categories},</if>",
            "<if test='tags !=  null'> tags=#{tags},</if>",
            "<if test='allowComment !=  null'> allow_comment=#{allowComment},</if>",
            "<if test='thumbnail !=  null'> thumbnail=#{thumbnail},</if>",
            "modifed = CURRENT_TIMESTAMP,"+
            "</set>",
            "WHERE id=#{id}",
            "</script>"
    })
    public Integer updateArticleWithId(BlogArticle article);
}

//import com.domain.Statistic;
//import org.apache.ibatis.annotations.*;
//
//import java.util.List;
//
//@Mapper
//public interface BlogArticleMapper {
//
//    // 根据ID查询统计信息
//    @Select("SELECT * FROM t_statistic WHERE id = #{id}")
//    Statistic findById(Integer id);
//
//    // 插入统计信息
//    @Insert("INSERT INTO t_statistic(article_id, hits) VALUES (#{articleId}, #{hits})")
//    int insertStatistic(Statistic statistic);
//
//    // 更新统计信息
//    @Update("UPDATE t_statistic SET article_id = #{articleId}, hits = #{hits} WHERE id = #{id}")
//    int updateStatistic(Statistic statistic);
//
//    // 根据ID删除统计信息
//    @Delete("DELETE FROM t_statistic WHERE id = #{id}")
//    int deleteStatisticById(Integer id);
//
//    // 查询所有统计信息
//    @Select("SELECT * FROM t_statistic")
//    List<Statistic> findAllStatistics();
//
//    // 根据文章ID查询统计信息
//    @Select("SELECT * FROM t_statistic WHERE article_id = #{articleId}")
//    Statistic findByArticleId(Integer articleId);
//
//}
